package com.yishan.aspect;

import com.yishan.service.LogService;
import com.yishan.service.OtherService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

/**
 * 日志切面
 *
 * @author: yishan
 * @Date: 2022/1/6 13:44
 **/
@Component
@Aspect
@Slf4j
public class LogAspect {

    @Autowired
    private LogService logService;

    @Autowired
    private OtherService otherService;

    /**
     * 开始时间
     */
    private long visitTime;
    /**
     * 访问的类
     */
    private Class clazz;

    /**
     * 访问的方法
     */
    private Method method;

    @Pointcut("execution(public * com.yishan.service.LogService.*(..))")
    private void pt() {

    }

    @Before("pt()")
    public void before(JoinPoint joinPoint) throws NoSuchMethodException {
        visitTime = System.currentTimeMillis();
        Date date = new Date(visitTime);

        clazz = joinPoint.getTarget().getClass();
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        if (args == null || args.length == 0) {
            method = clazz.getMethod(methodName);
        } else {
            Class[] argClasses = new Class[args.length];
            for (int i = 0; i < args.length; i++) {
                argClasses[i] = args[i].getClass();
            }
            method = clazz.getMethod(methodName, argClasses);
        }

        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();

        log.info("================请求开始=====================");
        String requestMethod = request.getMethod();
        log.info("请求时间:" + new SimpleDateFormat().format(date));
        log.info("请求IP:" + request.getRemoteAddr());
        log.info("请求地址:" + request.getRequestURI());
        log.info("请求方式:" + requestMethod);
        log.info("请求类方法:" + joinPoint.getSignature());
        log.info("请求类方法参数:" + Arrays.toString(joinPoint.getArgs()));
        log.info("================请求结束=====================");
    }

    @AfterReturning(returning = "obj",pointcut = "pt()")
    public void methodAfterReturn(Object obj) {
        long time = System.currentTimeMillis() - visitTime;
        log.info("执行时长:{}s", time / 1000);
        log.info("===================返回内容===========================");

        otherService.batchAdd();

        //try {
        //    log.info(logService.addLog());
        //} catch (Exception e) {
        //    e.printStackTrace();
        //    log.info("发生错误", e);
        //}
        log.info("===================返回内容===========================");
    }


}
